package model.geometry;

/**
 * Created by andreas on 20.04.14.
 */
public class DiamondDrawer extends AbstractDrawer {

    public String drawIsoTriangle(int lines) {
        return drawIsoTriangleRecursive(0, lines, "") + drawMiddleLine(lines, null);
    }

    public String drawDiamond(int lines) {
        return drawDiamond(lines, null);
    }

    public String drawDiamond(int lines, String name) {
        return drawIsoTriangleRecursive(0, lines, "") + drawMiddleLine(lines, name) + (lines == 1 ? "" : "\n") + drawIsoTriangleRecursiveBottom(lines-2, lines, "");
    }

    private String drawIsoTriangleRecursive(int i, int lines, String accumulator) {
        if (i == lines-1) {
            return accumulator;
        } else {
            final int width = calcWidth(lines);
            final int numAsterixes = 1 + 2 * i;
            final int numWhiteSpaces = (width - numAsterixes) / 2;
            final String whiteSpaces = drawWhitespaces(numWhiteSpaces);
            final String asterixes = drawAsterix(numAsterixes);
            final String line = whiteSpaces + asterixes + whiteSpaces + "\n";
            return drawIsoTriangleRecursive(i+1, lines, accumulator + line);
        }
    }

    private String drawIsoTriangleRecursiveBottom(int i, int lines, String accumulator) {
        if (i == -1) {
            return accumulator;
        } else {
            final int width = calcWidth(lines);
            final int numAsterixes = 1 + 2 * i;
            final int numWhiteSpaces = (width - numAsterixes) / 2;
            final String whiteSpaces = drawWhitespaces(numWhiteSpaces);
            final String asterixes = drawAsterix(numAsterixes);
            final String newLine = i > 0 ? "\n" : "";
            final String line = whiteSpaces + asterixes + whiteSpaces + newLine;
            return drawIsoTriangleRecursiveBottom(i-1, lines, accumulator + line);
        }
    }

    private String drawMiddleLine(int lines, String name) {
        int width = calcWidth(lines);
        return name == null ? drawAsterix(width) : name;
    }

    private int calcWidth(int lines) {
        return 1 + 2 * (lines - 1);
    }


}
